// pages/personal-team/personal-team.js

import api from '../../utils/request/api'
const util = require('../../utils/util.js');
import {
  userTeam,createwxaqrcode
} from '../../utils/request/apiurl'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeHidden: true,
    userData:'' ,
    uid: '',
    teamNum: 0,
    isRequest: false,
    searchGood: {
    	page: 1,
    	size: 10,
      pages: 0
    },
    dataList: [],
	createcode:'',//二维码
	// isColonel:0,//0不是团长
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
	  var userInfo=wx.getStorageSync('userInfo')
	  this.setData({
		  uid:userInfo.uid,
		  userData:wx.getStorageSync('Info')
		  // isColonel:userInfo.isColonel
	  })
	
    // 获取我推荐的人数
    api.post(userTeam.teamNum, {
      uid: this.data.uid||wx.getStorageSync('userInfo').uid
    }).then(res => {
      // console.log(res)
      this.setData({
        teamNum: res
      })
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
    this.getList()
	 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
	var userInfo=wx.getStorageSync('userInfo')
	this.setData({
		  uid:userInfo.uid,
		  // isColonel:userInfo.isColonel
	})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
  	wx.showNavigationBarLoading() //在标题栏中显示加载
	this.setData({
		dataList: [],
		searchGood: {
			page: 1,
			size: 10,
		  pages: 0
		},
	})
  	this.onLoad()
  	setTimeout(function() {
  		wx.hideNavigationBarLoading() //完成停止加载
  		wx.stopPullDownRefresh() //停止下拉刷新
  	}, 2000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
    this.setData({
      searchGood: {
        page: this.data.searchGood.page + 1,
        size: 10,
        pages: this.data.searchGood.pages
      }
    })
    	this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 二维码
  createwxaqrcode(){
	  var source={
		userId:this.data.uid||wx.getStorageSync('userInfo').uid,
		path:'pages/home/home',
	  }
	  api.post(createwxaqrcode.list, source).then(res => {
		// console.log(res)
		var str = res.replace(/\\/, '')
		this.setData({
				createcode:str
			})
	  }).catch(err => {
		// console.log(err)
		wx.showToast({
			title: err.message,
			icon: 'none'
		})
	  })
	  
  },
  // 搜索
  toSearch() {
    wx.navigateTo({
      url: '../good-search/good-search'
    })
  },
  // 打开二维码弹窗
  showCode() {
		  this.setData({
		    codeHidden: false
		  })
    
  },
  // 关闭二维码弹窗
  closeCode() {
    this.setData({
      codeHidden: true
    })
  },
  //获取我的推荐列表
  getList() {
    api.post(userTeam.list, {
      page: this.data.searchGood.page,
      size: this.data.searchGood.size,
      uid: wx.getStorageSync('userInfo').uid||this.data.uid
    }).then(res => {
      // console.log(res,'444')
      let list = res.rows
      list.map(a => {
        a.addTime = util.getLocalTime(a.addTime)
        return a
      })
	  let pages = res.total / this.data.searchGood.size
      this.setData({
        dataList: [...this.data.dataList,...list],
        searchGood: {
          total: pages,
          page: this.data.searchGood.page,
          size: this.data.searchGood.size,
        }
      })
	   this.createwxaqrcode()
      // console.log(this.data.dataList)
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  }
})